<?php
!defined('IN_FRAME') && die('404 Page');
class c_myself extends Controller {
    
    private $arrManagerData = null;
    private $mdlManager;
    
    public function __construct() {
        $mdlManager = Helper::loadModel('manager');
        if (!$mdlManager->logined()) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=login', '请先登录!');
        } else {
            $this->arrManagerData = $mdlManager->getSession();
            $this->mdlManager = Helper::loadModel('manager');
        }
    }
    
    public function password() {
        $arrAssign = array('strTitle' => '修改密码');
        if (isset($_POST['sub_btn'])) {
            $bln = true;
            $strMsg = '';
            if (!isset($_POST['password']) || empty($_POST['password'])) {
                $bln = false;
                $strMsg = '请输入当前密码!';
            }
            if ($bln && sha1($_POST['password']) != $this->arrManagerData['password']) {
                $bln = false;
                $strMsg = '当前密码错误,请确认!';
            }
            if ($bln && (!isset($_POST['password_1']) || !$this->mdlManager->isPassword($_POST['password_1']))) {
                $bln = false;
                $strMsg = '请输入由6~20位字母或数字组成的新密码!';
            }
            if ($bln && (!isset($_POST['password_2']) || $_POST['password_1'] != $_POST['password_2'])) {
                $bln = false;
                $strMsg = '请再输入一次新密码!';
            }
            if ($bln) {
                $this->mdlManager->update(array('password' => sha1($_POST['password_1'])), $this->arrManagerData['id']);
                $this->mdlManager->logout();
                Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=login', '密码修改成功,请用新密码重新登录!');
            }
            $arrAssign['strMsg'] = $strMsg;
        }
        $this->_display('admin/myself_pasd.php', $arrAssign);
    }
    
    public function index() {
        $arrAssign = array(
        	'strTitle' => '基本资料',
        	'arrData' => $this->mdlManager->getOne($this->arrManagerData['id'])
        );
        if (true) {
            $arrUpdate = array();
            $bln = true;
            $strMsg = '';
            if (isset($_POST['nick_name']) && !empty($_POST['nick_name'])) {
                if (!$this->mdlManager->isNickName($_POST['nick_name'])) {
                    $bln = false;
                    $strMsg = '昵称由3~20个字符组成!';
                } else {
                    $arrUpdate['nick_name'] = $_POST['nick_name'];
                }
            }
            if ($bln && isset($_POST['email']) && !empty($_POST['email'])) {
                if (!Helper::isEmail($_POST['email'])) {
                    $bln = false;
                    $strMsg = 'Email格式错误!';
                } else {
                    $arrUpdate['email'] = $_POST['email'];
                }
            }
            if ($bln && isset($_POST['phone']) && !empty($_POST['phone'])) {
                if (!$this->mdlManager->isPhone($_POST['phone'])) {
                    $bln = false;
                    $strMsg = '请输入正确的固定电话或手机号码!';
                } else {
                    $arrUpdate['phone'] = $_POST['phone'];
                }
            }
            if ($bln && count($arrUpdate) > 0) {
                $this->mdlManager->update($arrUpdate, $this->arrManagerData['id']);
                $arrAssign['strOk'] = '更新成功!';
                $arrAssign['arrData'] = $arrUpdate;
            }
            $arrAssign['strMsg'] = $strMsg;
        }
        $this->_display('admin/myself_index.php', $arrAssign);
    }
}